from collections import defaultdict
from random import uniform
from math import sqrt

ids = defaultdict(int)
adj_list = defaultdict(set)

vertices = set([])

locations = defaultdict(tuple)

data_file = 'data/test_data.txt'



def get_ids():
    data = open(data_file, 'r')
    line = data.readline()
    while line:
        l = line.strip()
        if l:
            vals = l.split()
            if (ids.get(vals[0].lower()+';n', -1) == -1):
                ids[vals[0].lower()+';n'] = len(ids)
            if (ids.get(vals[1].lower()+';v', -1) == -1):
                ids[vals[1].lower()+';v'] = len(ids)
        line = data.readline()
    data.close()

def get_adj_lists():
    data = open(data_file, 'r')
    line = data.readline()
    while line:
        l = line.strip()
        if l:
            vals = l.split()
            noun = ids[vals[0].lower()+';n']
            verb = ids[vals[1].lower()+';v']
            adj_list[noun].add(verb)
            adj_list[verb].add(noun)
        line = data.readline()
    data.close()

def get_positions(x_cord, y_cord):
    v = vertices.pop()
    locations[v] = (x_cord,y_cord)
    for i in adj_list[v]:
        x = uniform(-1,1)
        y = uniform(-1,1)
        temp = sqrt(x*x + y*y)
        x = x/temp
        y = y/temp
        locations[i] = (x,y)
        vertices.remove(i)
    for i in adj_list[v]:
        get_positions2(i, x_cord, y_cord)
    if len(vertices):
        get_positions(x_cord+100, y_cord + 100)

def get_positions2(j, x_center, y_center):
    for i in adj_list[j]:
        if (i not in vertices):
            i_loc = locations[i]
            j_loc = locations[j]
            new_loc = ((i_loc[0]+j_loc[0])/2, (i_loc[1]+j_loc[1])/2)
            locations[j] = new_loc
    temp_j_loc = (locations[j][0]-x_center, locations[j][1]-y_center)
    for i in adj_list[j]:
        if (i in vertices):
            x = uniform(-1,1)
            y = uniform(-1,1)
            temp = sqrt(x*x + y*y)
            x = x/temp
            y = y/temp
            x = x+temp_j_loc[0]
            y = y+temp_j_loc[1]
            j_dist = temp_j_loc[0]*temp_j_loc[0] + temp_j_loc[1]*temp_j_loc[1]
            i_dist = x*x + y*y
            while(j_dist >= i_dist):
                x = uniform(-1,1)
                y = uniform(-1,1)
                temp = sqrt(x*x + y*y)
                x = x/temp
                y = y/temp
                x = x+temp_j_loc[0]
                y = y+temp_j_loc[1]
                i_dist = x*x + y*y
            locations[i] = (x+x_center,y+y_center)
            vertices.remove(i)
            get_positions2(i, x_center, y_center)
    
def print_adj_matrix():
    out = open('data/adj_matrix.txt', 'w')
    for i in range(len(adj_list)):
        for j in range(len(adj_list)):
            if (j in adj_list[i]):
                out.write('1 ')
            else:
                out.write('0 ')
        out.write('\n')
    out.close()

def print_loc():
    out = open('data/locations.txt', 'w')
    for i in range(len(adj_list)):
        out.write('{0} {1}\n'.format(locations[i][0], locations[i][1]))

def evaluate():
    global vertices
    get_ids()
    get_adj_lists()
    vertices = set(range(len(ids)))
    get_positions(0,0)
    print_adj_matrix()
    print_loc()
    
evaluate()
